package com.springdemo.controller;

import com.springdemo.bean.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("com")
public class UserController {

    @GetMapping("/test")
    public void test() {

    }

    @GetMapping("/test2")
    public String test2() {
        return "hello world";
    }

    @GetMapping("/test3")
    public User test3() {
        return getUsers().get(0);
    }

    @GetMapping("/test4")
    public List<User> test4() {
        return getUsers();
    }

    @GetMapping("/test5/{id}")
    public User test5(@PathVariable("id") int id) {
        return getUsers().get(id);
    }

    @GetMapping("/login")
    public String login(@RequestParam("name") String name,
                        @RequestParam("password") String password) {
        return name + " " + password;
    }

    /**
     * 参数限制
     *
     * @param name     defaultValue = "小明"
     * @param password
     * @return
     */
    @GetMapping("/login2")
    public String login2(@RequestParam(value = "name", defaultValue = "小明") String name,
                         @RequestParam("password") String password) {
        return name + " " + password;
    }

    /**
     * post请求，请求体放在body中
     *
     * @param user
     * @return
     */
    @PostMapping("/test6")
    public User test6(@RequestBody User user) {
        return user;
    }

    public List<User> getUsers() {
        List<User> userList = new ArrayList<>();

        User u1 = new User();
        u1.setId(1);
        u1.setName("张三");
        u1.setAge(18);

        User u2 = new User();
        u2.setId(2);
        u1.setName("李四");
        u1.setAge(20);

        userList.add(u1);
        userList.add(u2);

        return userList;
    }

}
